#include "const.h"

var_t g_var_root = (var_t)0;
var_t g_var_char[256];

void v_init() {
	size_t i;
	var_t v;

	atexit(v_terminate);

	g_var_root = (var_t)i_new(sizeof(*g_var_root));

	g_var_root->name = (char *)0;
	g_var_root->hash = 0;
	g_var_root->flags = 0;

	g_var_root->value.text = (char *)0;
	g_var_root->expanded = (char *)0;

	g_var_root->array.members = (char **)0;
	g_var_root->array.n_text[0] = 0;
	g_var_root->array.i_text[0] = 0;
	g_var_root->array.capacity = 0;
	g_var_root->array.n = 0;
	g_var_root->array.i = 0;
	g_var_root->array.ptrvar = (var_t)0;

	for(i=0; i<VAR_MAX_INHERITENCE; i++)
		g_var_root->i.owners[i] = (var_t)0;
	g_var_root->i.owned_index = 0;
	g_var_root->i.owned = (var_t)0;

	g_var_root->h.prnt = (var_t)0;
	g_var_root->h.prev = (var_t)0;
	g_var_root->h.next = (var_t)0;
	g_var_root->h.head = (var_t)0;
	g_var_root->h.tail = (var_t)0;

	for(i=0; i<sizeof(g_var_char)/sizeof(g_var_char[0]); i++)
		g_var_char[i] = (var_t)0;

	v = v_new_array("outfiles", "outfile", 8);
	v = v_new_array("infiles", "infile", 64);
	v = v_new_array("indepfiles", "indepfile", 128);

	v = v_new_array("srcdirs", "srcdir", 32);
	v = v_new_array("incdirs", "incdir", 32);
	v = v_new_array("libdirs", "libdir", 32);

	v = v_new_array("srcfiles", "srcfile", 256);
	v = v_new_array("objfiles", "objfile", 256);
	v = v_new_array("depfiles", "depfile", 256);

	v = v_new("projroot");

	v = v_new("config");

	v = v_new_const("hostsys");
	v = v_new_symlink("host", v);
	v = v_new_const("hostarch");

	v = v_new_array("targets", "target", 8);
	v = v_new("targetsys");
	v = v_new("targetarch");

	g_var_char['@'] = v_new_symlink("@", v_find("outfile"));
	g_var_char['<'] = v_new_symlink("<", v_find("infile"));
	g_var_char['+'] = v_new_symlink("+", v_find("infiles"));
	g_var_char['>'] = v_new_symlink(">", v_find("indepfile"));

#if HOST_OS==OS_MSWIN
	v_setvar("hostsys", "mswin");
#elif HOST_OS==OS_LINUX
	v_setvar("hostsys", "linux");
#elif HOST_OS==OS_MACOS
	v_setvar("hostsys", "macos");
#else
	v_setvar("hostsys", "unknown");
#endif

#if HOST_ARCH==ARCH_X86
	v_setvar("hostarch", "x86");
#elif HOST_ARCH==ARCH_AMD64
	v_setvar("hostarch", "amd64");
#elif HOST_ARCH==ARCH_ARM
	v_setvar("hostarch", "arm");
#elif HOST_ARCH==ARCH_ARM64
	v_setvar("hostarch", "arm64");
#elif HOST_ARCH==ARCH_PPC
	v_setvar("hostarch", "ppc");
#elif HOST_ARCH==ARCH_SPARC
	v_setvar("hostarch", "sparc");
#elif HOST_ARCH==ARCH_MIPS
	v_setvar("hostarch", "mips");
#else
	v_setvar("hostarch", "unknown");
#endif

	v_setvar("builder.version", va("%d.%.2d [%s]",
		VERSION_MAJOR, VERSION_MINOR, __DATE__));
	v_setvar("builder.version.number", va("%d", VERSION));
	v_setvar("builder.version.major", va("%d", VERSION_MAJOR));
	v_setvar("builder.version.minor", va("%d", VERSION_MINOR));
	v_setvar("builder.version.date", __DATE__);
}
